// SDL2_02 [Moving Rectangle].nova // Simple SDL2 example with moving rectangle. // Using namespace declarations. using library.emscripten; using library.sdl2; // The application class. class SDL2_02_Moving_Rectangle { // Static data members. private static SDL_Window w; private static SDL_Renderer r; private static SDL_Rect rect; // Application class's "main" function. public static void main( String[] args ) { Stream.writeLine( "SDL2_02 [Moving Rectangle].nova" ); // Disable the emscripten full-screen controls. Emscripten.disableControls( ); // Setup SDL. SDL2.SDL_Init( SDL2.SDL_INIT_VIDEO ); w = SDL2.SDL_CreateWindow( "SDL2_02_MovingRectangle", SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, 640, 480, SDL2.SDL_WINDOW_SHOWN ); // Check for a null reference. if ( w == null ) { // Output an error message. Stream.writeLine( "Failed to create window: " + SDL2.SDL_GetError( ) ); // Abort the application. return; } r = SDL2.SDL_CreateRenderer( w, -1, SDL2.SDL_RENDERER_ACCELERATED | SDL2.SDL_RENDERER_PRESENTVSYNC ); // Define the rectangle. rect = new SDL_Rect( -80, 100, 80, 80 ); // Check for the emscripten environment. if ( Emscripten.isActive( ) ) { int simulate_infinite_loop = 1; // Call the function repeatedly. int fps = -1; // Call the function as fast as the browser wants to render (typically 60fps). Emscripten.setMainLoop( renderFrame, fps, simulate_infinite_loop ); } else { // Rendering and event processing loop. do { SDL_Event e = SDL2.SDL_PollEvent( ); if ( e != null ) if ( e.id == SDL2.SDL_QUIT ) break; renderFrame( ); } while( true ); } // Shutdown app. SDL2.SDL_DestroyRenderer( r ); SDL2.SDL_DestroyWindow( w ); SDL2.SDL_Quit( ); } public static void renderFrame( ) { // Set the background colour (dark blue). SDL2.SDL_SetRenderDrawColor( r, 0, 0, 128, 255 ); // Clear the window. SDL2.SDL_RenderClear( r ); // Set the rectangle colour (orange). SDL2.SDL_SetRenderDrawColor( r, 255, 128, 0, 255 ); // Render the rectangle. SDL2.SDL_RenderFillRect( r, rect ); // Update the screen. SDL2.SDL_RenderPresent( r ); // Move the rectangle and wraparound if required. if ( ++rect.x > 640 ) rect.x = -80; } }